//
//  PushPicNR.m
//  HYHoney
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PushPicNR.h"
#import "ModelOfPic.h"
#import "PicLayout.h"
#import "PicHeader.h"
#import "NetworkHandeler.h"
#import "UIImageView+WebCache.h"
#import "DKNightVersion.h"

@interface PushPicNR ()<UITableViewDelegate, UITableViewDataSource, NetworkHandelerDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *arr;

@end

@implementation PushPicNR


- (void)dealloc{

    [self.tableView release];
    [self.arr release];
    [self.array release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
    [self handleData];
    
    NSLog(@"%ld", (long)self.idNumber);
}


- (void)createTableView{

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView release];
    
    [self.tableView registerClass:[PicLayout class] forCellReuseIdentifier:@"pool3"];
    [self.tableView registerClass:[PicHeader class] forHeaderFooterViewReuseIdentifier:@"pool4"];  // tableview 头部
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xFFFAFA, 0x27408B);
    self.tableView.showsVerticalScrollIndicator = NO;
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


- (void)handleData{
    
    NSString *str = [NSString stringWithFormat:@"http://appapi.17house.com/xiaoguotuApi.php?action=albumDetail&id=%ld",(long)self.idNumber];
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}


- (void)handelerDidComplete:(id)result{
    
    self.arr = [NSMutableArray array];
    self.array = [NSMutableArray array];
    
    NSDictionary *dictionary = [result objectForKey:@"data"];
    
    ModelOfPic *model = [[ModelOfPic alloc] init];
    
    model.name = [dictionary objectForKey:@"name"];
    model.desc = [dictionary objectForKey:@"desc"];
    
    [self.array addObject:model];
    
    
    NSArray *arr = [dictionary objectForKey:@"images"];
    
    for (NSDictionary *dic in arr) {
        
        ModelOfPic *model2 = [[ModelOfPic alloc] init];
        
        model2.path = [dic objectForKey:@"path"];
        model2.like_desc = [dic objectForKey:@"like_desc"];
        
        [self.arr addObject:model2];
    }
    
    [self.tableView reloadData];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arr.count;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    PicHeader *picHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"pool4"];
    
    ModelOfPic *model = [self.array objectAtIndex:section];
    
    picHeader.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    
    [picHeader passModel:model];
        
    return picHeader;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PicLayout *cell = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
    
    ModelOfPic *model2 = [self.arr objectAtIndex:indexPath.item];
    
    [cell passmodel:model2];
    
    
    return cell;
}




- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return [PicHeader heightForCell:self.array[section]];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [PicLayout heightForCell:self.arr[indexPath.item]];
}


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
